/** 
Copyright (c) 2010, Sheikh Abdul Wahid Ahmed
Details @ http://extremecodeworld.codeplex.com/license
**/

using System;
using System.Data;
using System.Text;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EXtremecode.PresentationLayer;

public partial class ASCXCheckBox : System.Web.UI.UserControl, IASCXControl
{
    private bool isSetDisplayMethodCalled = false;

    private string checkedImageUrl = "~/Images/Checked.gif";
    public string CheckedImageUrl
    {
        get { return checkedImageUrl; }
        set { checkedImageUrl = value; }
    }
    
    private string uncheckedImageUrl = "~/Images/Unchecked.gif";
    public string UncheckedImageUrl
    {
        get { return uncheckedImageUrl; }
        set { uncheckedImageUrl = value; }
        
    }


    public override string ClientID
    {
        get
        {
            return CurrentClientId;
        }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        //Script block
        string currentClientId = this.CurrentClientId;
        string originalClientId = this.OriginalClientId;
        string inputClientId = this.InputClientId;
        string displayClientId = this.DisplayClientId;

        StringBuilder sbScriptBlock = new StringBuilder();

        sbScriptBlock.AppendLine("<script>");
        sbScriptBlock.AppendLine(
            string.Format("var {0} = document.getElementById('{0}');", currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.OriginalCtrl = document.getElementById('{1}');",
                currentClientId, originalClientId));

        if (!IsReadOnly)
        {
            sbScriptBlock.AppendLine(
                string.Format("var {0} = document.getElementById('{0}');", inputClientId));

            sbScriptBlock.AppendLine(
               string.Format("{0}.CurrentCtrl = document.getElementById('{1}');",
                   inputClientId, currentClientId));

            if (displayCheckBoxCaption)
            { 
                checkBox.Text = caption; 
            }
            else
            {
                checkBox.Text = string.Empty;
            }
        }

        sbScriptBlock.AppendLine(
            string.Format("{0}.InputCtrl = document.getElementById('{1}');",
            currentClientId, inputClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.DisplayCtrl = document.getElementById('{1}');",
            currentClientId, displayClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.CheckedImage = new Image();", currentClientId));

        sbScriptBlock.AppendLine(
            string.Format("{0}.CheckedImage.src = '{1}';",
            currentClientId, ResolveClientUrl(checkedImageUrl)));

        sbScriptBlock.AppendLine(
    string.Format("{0}.UncheckedImage = new Image();", currentClientId));


        sbScriptBlock.AppendLine(
            string.Format("{0}.UncheckedImage.src = '{1}';",
            currentClientId, ResolveClientUrl(uncheckedImageUrl)));

        
        sbScriptBlock.AppendLine(
            string.Format("{0}.SetValue = SetCheckBoxValue;", currentClientId));


        sbScriptBlock.AppendLine(
            string.Format("{0}.GetValue = GetCheckBoxValue;", currentClientId));


        sbScriptBlock.AppendLine(
            string.Format("{0}.ResetValue = resetValue;", currentClientId));

        sbScriptBlock.AppendLine("</script>");

        scriptBlockLiteral.Text = sbScriptBlock.ToString();


        if (!isSetDisplayMethodCalled)
        {
            SetDisplay();
        }



    }
    private void SetDisplay()
    {
        //set Image.
        if (IsReadOnly)
        {
            if (hdnCurrent.Value == "true")
            {
                image.ImageUrl = checkedImageUrl;
            }
            else
            {
                image.ImageUrl = uncheckedImageUrl;
            }
        }
    }
    public bool? OriginalValue
    {
        get
        {
            bool val = (hdnOriginal.Value == "true" || hdnOriginal.Value == "1") ? true : false;
            return (!val && isFalseMeansNull) ?
                null :
                (bool?)val;
        }
    }
    public bool? Value
    {
        get
        {
            bool val = (isReadOnly && !checkBox.Checked ) ?
                ((hdnCurrent.Value == "true" || hdnCurrent.Value == "1") ? true : false) :
                checkBox.Checked;

            return (!val && isFalseMeansNull) ?
                null :
                (bool?) val;
            
        }
        set
        {
            checkBox.Checked = (value == null) ? false : value.Value;

            hdnCurrent.Value =
                hdnOriginal.Value =
                checkBox.Checked.ToString().ToLower();

            SetDisplay();
            isSetDisplayMethodCalled = true;
            
            
        }
    }
    private bool isFalseMeansNull = true;
    public bool IsFalseMeansNull
    {
        get { return isFalseMeansNull; }
        set { isFalseMeansNull = value; }
    }
    

    #region IASCXControl Members
    public bool HasValueChanged
    {
        get
        {
            return (Value != OriginalValue);
        }
    }
    public string DisplayCssClass
    {
        get
        {
            return image.CssClass;
        }
        set
        {
            image.CssClass = value;
        }
    }
    
    private bool isReadOnly = false;
    public bool IsReadOnly
    {
        get
        {
            return isReadOnly;
        }
        set
        {
            isReadOnly = value;
            if (isReadOnly)
            {
               multiView.SetActiveView(viewReadOnly);
            }
            else
            {
                multiView.SetActiveView(viewEditable);
            }
        }
    }
    object IASCXControl.Value
    {
        get
        {
            return Value;
        }
        set
        {
            Value = value as bool?;
        }
    }
    object IASCXControl.OriginalValue
    {
        get
        {
            return OriginalValue;
        }
        
    }
    public string Text
    {
        get
        {
            bool? val = Value;
            string str = string.Empty;
            if (val == null)
            {
                str = string.Empty;
            }
            else if (val == true)
            {
                str = "Yes";
            }
            else
            {
                str = "No";
            }
            return str;
        }
    }

    public string InputClientId
    {
        get
        {
            return checkBox.ClientID;
        }
    }
    public string InputUniqueId
    {
        get
        {
            return checkBox.UniqueID;
        }
    }

    public string DisplayClientId
    {
        get
        {
            return image.ClientID;
        }
    }
    public string OriginalClientId
    {
        get
        {
            return hdnOriginal.ClientID;
        }
    }
    public string CurrentClientId
    {
        get
        {
            return hdnCurrent.ClientID;
        }
    }
    public string DisplayUniqueId
    {
        get
        {
            return image.UniqueID;
        }
    }

    public Control InputControl
    {
        get
        {
            return checkBox;
        }
    }

    public string JSResetFunction
    {
        get
        {
            return "ResetCheckBox";
        }
    }

    private bool isRequired=false;
    public bool IsRequired
    {
        get
        {
            return isRequired;
        }
        set
        {
            isRequired = value;
        }
    }

    private string caption = string.Empty;
    public string Caption
    {
        get
        {
            return caption;
        }
        set
        {
            caption = value;
        }
    }

    private bool displayCheckBoxCaption = false;
    public bool DisplayCheckBoxCaption
    {
        get
        {
            return displayCheckBoxCaption;
        }
        set
        {
            displayCheckBoxCaption = value;
        }
    }



    #endregion

       
    
}
